package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.StrategyCommend;
import cn.wolfcode.luowowo.article.domain.TravelCommend;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.ITravelCommendService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.search.domain.DestinationEs;
import cn.wolfcode.luowowo.search.domain.StrategyEs;
import cn.wolfcode.luowowo.search.domain.TravelEs;
import cn.wolfcode.luowowo.search.domain.UserInfoEs;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.vo.SearchResult;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class IndexController {

    @Reference
    private IUserInfoRedisService userInfoRedisService;


    @Reference
    private ITravelCommendService travelCommendService;

    @Reference
    private IStrategyCommendService strategyCommendService;

    @Reference
    private ITravelService travelService;

    @Reference
    private IDestinationEsService destinationEsService;

    @Reference
    private IStrategyEsService strategyEsService;

    @Reference
    private ITravelEsService travelEsService;

    @Reference
    private IUserInfoEsService userInfoEsService;

    @Reference
    private ISearchService searchService;

    /**
     * 首页
     * @param model
     * @return
     */
    @GetMapping("")
    public String toIndex(Model model){
//        String token = CookieUtil.getToken(request);
//
//        UserInfo userInfo = userInfoRedisService.getUserByToken(token);
//
//        request.getSession().setAttribute("userInfo",userInfo);

        //tcs
        List<TravelCommend> top5 = travelCommendService.getTop5();

        model.addAttribute("tcs",top5);

        //sc
        StrategyCommend sc = strategyCommendService.getTop5().get(0);
        model.addAttribute("sc",sc);

        return "index/index";
    }

    @RequestMapping("/index/travelPage")
    public String travelPage(Model model, @ModelAttribute("qo")TravelQuery qo){
        PageInfo pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "index/travelPageTpl";
    }


    @RequestMapping("/q")
    public String seacrh(Model model, @ModelAttribute("qo")SearchQueryObject qo){
        switch (qo.getType()){
            case SearchQueryObject.SEARCH_TYPE_USER:
                return this.seacrhUser(model,qo);
            case SearchQueryObject.SEARCH_TYPE_DEST:
                return this.seacrhDest(model,qo);
            case SearchQueryObject.SEARCH_TYPE_STRATEGY:
                return this.seacrhStrategy(model,qo);
            case SearchQueryObject.SEARCH_TYPE_TRAVEL:
                return this.seacrhTravel(model,qo);
            default:
                return this.seacrhAll(model,qo);
        }
    }

    private String seacrhUser(Model model, SearchQueryObject qo) {
        Page<UserInfoEs> us = searchService.searchWithHighlight(UserInfoEs.INDEX_NAME, UserInfoEs.TYPE_NAME,
                UserInfoEs.class, qo, "nickName", "destName");

        model.addAttribute("page",us);
        return "index/searchUser";
    }

    private String seacrhTravel(Model model, SearchQueryObject qo) {
        Page<TravelEs> ts = searchService.searchWithHighlight(TravelEs.INDEX_NAME, TravelEs.TYPE_NAME,
                TravelEs.class, qo, "title",  "summary");

        model.addAttribute("page",ts);

        return "index/searchTravel";
    }

    private String seacrhStrategy(Model model, SearchQueryObject qo) {

        Page<StrategyEs> sts = searchService.searchWithHighlight(StrategyEs.INDEX_NAME, StrategyEs.TYPE_NAME,
                StrategyEs.class, qo, "title", "subTitle", "summary");

        model.addAttribute("page",sts);

        return "index/searchStrategy";
    }

    private String seacrhDest(Model model, SearchQueryObject qo) {

        DestinationEs destinationEs = destinationEsService.findByName(qo.getKeyword());

        if(destinationEs != null){
            //查询该目的下的所有攻略 游记 用户
            List<StrategyEs> sts = strategyEsService.findByDestName(destinationEs.getName());
            List<TravelEs> ts = travelEsService.findByDestName(destinationEs.getName());
            List<UserInfoEs> us = userInfoEsService.findByDestName(destinationEs.getName());

            SearchResult searchResult = new SearchResult();
            searchResult.setStrategys(sts);
            searchResult.setTravels(ts);
            searchResult.setUsers(us);
            long total = sts.size() + ts.size() + us.size();
            searchResult.setTotal(total);
            model.addAttribute("data",searchResult);
        }
        model.addAttribute("dest",destinationEs);
        return "index/searchDest";
    }

    private String seacrhAll(Model model, SearchQueryObject qo) {

        Page<UserInfoEs> us = searchService.searchWithHighlight(UserInfoEs.INDEX_NAME, UserInfoEs.TYPE_NAME,
                UserInfoEs.class, qo, "nickName", "destName");

        Page<TravelEs> ts = searchService.searchWithHighlight(TravelEs.INDEX_NAME, TravelEs.TYPE_NAME,
                TravelEs.class, qo, "title",  "summary");

        Page<StrategyEs> sts = searchService.searchWithHighlight(StrategyEs.INDEX_NAME, StrategyEs.TYPE_NAME,
                StrategyEs.class, qo, "title", "subTitle", "summary");

        Page<DestinationEs> dests = searchService.searchWithHighlight(DestinationEs.INDEX_NAME, DestinationEs.TYPE_NAME,
                DestinationEs.class, qo, "name","info");

        SearchResult searchResult = new SearchResult();
        searchResult.setUsers(us.getContent());
        searchResult.setStrategys(sts.getContent());
        searchResult.setTravels(ts.getContent());
        searchResult.setDests(dests.getContent());
        searchResult.setTotal(us.getTotalElements()+sts.getTotalElements()+ts.getTotalElements()+dests.getTotalElements());

        model.addAttribute("data",searchResult);
        return "index/searchAll";
    }
}
